package com.zhg.hx.generator;

import java.io.File;

/**
 * Java菲波那契生成器
 * Generable对象可以在foreach语句中迭代
 * 使用迭代器惯用法，两个匿名内部类
 * Created by zhenghegong on 2019/12/3.
 */
public class Test {

    public static Generable<File> test(String path) {
        return new Generable<File>() {
            File file = new File(path);
            int currIndex = 0;

            @Override
            public Generator<File> generator() {
                return new Generator<File>() {

                    @Override
                    public boolean hasNext() {
                        File[] files = file.listFiles();
                        if (currIndex < files.length) {
                            return true;
                        } else {
                            return false;
                        }
                    }

                    @Override
                    public File next() {
                        File[] files = file.listFiles();
                        File nextFile = files[currIndex];
                        currIndex++;
                        return nextFile;
                    }
                };
            }
        };
    }

    public static void main(String[] args) {
        String property = System.getProperty("user.dir");
        for (File file : test(property)) {
            System.out.println(file.getPath());
        }
    }

}
